/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package org.open.oasis.docs.wsbpel._2._0.plnktype.impl;

import java.util.Collection;
import java.util.Iterator;

import javax.xml.namespace.QName;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

import org.eclipse.wst.wsdl.internal.impl.ExtensibilityElementImpl;
import org.open.oasis.docs.wsbpel._2._0.plnktype.PlnktypeFactory;
import org.open.oasis.docs.wsbpel._2._0.plnktype.PlnktypePackage;
import org.open.oasis.docs.wsbpel._2._0.plnktype.TPartnerLinkType;
import org.open.oasis.docs.wsbpel._2._0.plnktype.TRole;
import org.w3c.dom.Element;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>TPartner Link Type</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link org.open.oasis.docs.wsbpel._2._0.plnktype.impl.TPartnerLinkTypeImpl#getRole <em>Role</em>}</li>
 *   <li>{@link org.open.oasis.docs.wsbpel._2._0.plnktype.impl.TPartnerLinkTypeImpl#getName <em>Name</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class TPartnerLinkTypeImpl extends ExtensibilityElementImpl implements TPartnerLinkType {
	/**
	 * The cached value of the '{@link #getRole() <em>Role</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getRole()
	 * @generated
	 * @ordered
	 */
	protected EList<TRole> role;

	/**
	 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected String name = NAME_EDEFAULT;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected TPartnerLinkTypeImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return PlnktypePackage.Literals.TPARTNER_LINK_TYPE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<TRole> getRole() {
		if (role == null) {
			role = new EObjectContainmentEList<TRole>(TRole.class, this, PlnktypePackage.TPARTNER_LINK_TYPE__ROLE);
		}
		return role;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getName() {
		return name;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setName(String newName) {
		String oldName = name;
		name = newName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, PlnktypePackage.TPARTNER_LINK_TYPE__NAME, oldName, name));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case PlnktypePackage.TPARTNER_LINK_TYPE__ROLE:
				return ((InternalEList<?>)getRole()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case PlnktypePackage.TPARTNER_LINK_TYPE__ROLE:
				return getRole();
			case PlnktypePackage.TPARTNER_LINK_TYPE__NAME:
				return getName();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case PlnktypePackage.TPARTNER_LINK_TYPE__ROLE:
				getRole().clear();
				getRole().addAll((Collection<? extends TRole>)newValue);
				return;
			case PlnktypePackage.TPARTNER_LINK_TYPE__NAME:
				setName((String)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case PlnktypePackage.TPARTNER_LINK_TYPE__ROLE:
				getRole().clear();
				return;
			case PlnktypePackage.TPARTNER_LINK_TYPE__NAME:
				setName(NAME_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case PlnktypePackage.TPARTNER_LINK_TYPE__ROLE:
				return role != null && !role.isEmpty();
			case PlnktypePackage.TPARTNER_LINK_TYPE__NAME:
				return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (name: ");
		result.append(name);
		result.append(')');
		return result.toString();
	}

	/**
	 * Override the XML element token.
	 */
	public QName getElementType() {
		if (elementType == null)
			elementType = new QName(PlnktypePackage.eINSTANCE.getNsURI(),
					PlnktypePackage.eINSTANCE.getDocumentRoot_PartnerLinkType().getName());
		return elementType;
	}
	
	//
	// Reconcile methods: DOM -> Model
	//
	
	public void reconcileAttributes(Element changedElement) {
		super.reconcileAttributes(changedElement);		
		setName(changedElement.getAttribute("name"));
		reconcileReferences(false); // TODO true?
	}

	public void reconcileReferences(boolean deep) {
		super.reconcileReferences(deep);
	}

	public void handleUnreconciledElement(Element child,
			Collection remainingModelObjects) {

		if(child.getLocalName().equals(PlnktypePackage.eINSTANCE.getDocumentRoot_Role().getName())) {
			TRole role=PlnktypeFactory.eINSTANCE.createTRole();
			//role.setEnclosingDefinition(getEnclosingDefinition());
			role.setElement(child);
			getRole().add(role);
		}
	}

	protected void handleReconciliation(Collection remainingModelObjects) {
		for (Iterator i = remainingModelObjects.iterator(); i.hasNext();) {
			remove(this, i.next());
		}
	}

	protected void remove(Object component, Object modelObject) {
		TPartnerLinkType plt = (TPartnerLinkType) component;
		if (modelObject instanceof TRole) {
			plt.getRole().remove(modelObject);
		}
	}

	//
	// For reconciliation: Model -> DOM
	//

	protected void changeAttribute(EAttribute eAttribute) {
		if (isReconciling)
			return;

		super.changeAttribute(eAttribute);
		Element theElement = getElement();
		if (theElement != null) {
			if (eAttribute == null
					|| eAttribute == PlnktypePackage.eINSTANCE.getTPartnerLinkType_Name())
				niceSetAttribute(theElement,
						PlnktypePackage.eINSTANCE.getTPartnerLinkType_Name().getName(), getName());
		}
	}

	protected void changeReference(EReference eReference) {
		if (isReconciling)
			return;

		super.changeReference(eReference);
		Element theElement = getElement();
		if (theElement != null) {
			if (eReference == null
					|| eReference == PlnktypePackage.eINSTANCE.getTPartnerLinkType_Role()
							) {
				// TODO: Should I do something here?
				//                List roles = getRole();
				//                for (Iterator i = roles.iterator(); i.hasNext();)
				//                {
				//                    Role role = (Role) i.next();
				//                }
			}
		}
	}

	public Element createElement() {
		Element newElement = super.createElement();

		for (Iterator i = getRole().iterator(); i.hasNext();) {
			Object obj = i.next();
			if (obj instanceof TRole) {
				TRole role = (TRole) obj;
				Element child = ((TRoleImpl) role).createElement();
				newElement.appendChild(child);
			}
		}

		return newElement;
	}

	
} //TPartnerLinkTypeImpl
